package org.authentication.server.validate.smscode;



import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.util.Assert;

public class SmsAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
	
	public static final String MOBILE_KEY = "mobile";

	private String mobileParameter = MOBILE_KEY;
	private boolean postOnly = true;

	public SmsAuthenticationFilter() {
        super(new AntPathRequestMatcher("/login/mobile", "POST"));
    }
	
	@Override
	public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
			throws AuthenticationException, IOException, ServletException {
		if (postOnly && !request.getMethod().equals("POST")) {
            throw new AuthenticationServiceException(
                    "Authentication method not supported: " + request.getMethod());
        }
		
		 String mobile = obtainMobile(request);

	        if (mobile == null) {
	            mobile = "";
	        }

	        mobile = mobile.trim();

	        SmsAuthenticationToken authRequest = new SmsAuthenticationToken(mobile);

	        setDetails(request, authRequest);

	        return this.getAuthenticationManager().authenticate(authRequest);
	}
	 protected String obtainMobile(HttpServletRequest request) {
	        return request.getParameter(mobileParameter);
	    }

	    protected void setDetails(HttpServletRequest request,
	                              SmsAuthenticationToken authRequest) {
	        authRequest.setDetails(authenticationDetailsSource.buildDetails(request));
	    }

	    public void setMobileParameter(String mobileParameter) {
	        Assert.hasText(mobileParameter, "mobile parameter must not be empty or null");
	        this.mobileParameter = mobileParameter;
	    }

	    public void setPostOnly(boolean postOnly) {
	        this.postOnly = postOnly;
	    }

	    public final String getMobileParameter() {
	        return mobileParameter;
	    }
}
